package com.web.dao;

import com.web.bean.系别;
import com.jdbc.DataSourceTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class 系别Dao {
    //增
    public boolean insertDepartment(系别 dep) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "insert into 系别(院系代号,院系名称,备注)" +
                "values (?,?,?)";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,dep.get院系代号());
            ps.setString(2,dep.get院系名称());
            ps.setString(3, dep.get备注());

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //删
    public boolean deleteDepartment(String 院系代号) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "delete from 系别 where 院系代号 = ?";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,院系代号);

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }

    //改
    public boolean updateDepartment(系别 dep) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "update 系别 set 院系名称=?,备注=? where 院系代号=?; ";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,dep.get院系名称());
            ps.setString(2,dep.get备注());
            ps.setString(3,dep.get院系代号());
            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //查 -- 返回结果集
    public List<系别> queryDepartment() throws SQLException {
        //创建集合用于保存数据
        List<系别> list = new ArrayList<>();
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "select * from 系别;";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //执行sql
            ResultSet rs = ps.executeQuery();
            //遍历结果集
            while(rs.next()){
                //创建一个系别类型的数据用于获取数据库中相同类型的数据
                系别 dep=new 系别();
                dep.set院系代号(rs.getString(1));
                dep.set院系名称(rs.getString(2));
                dep.set备注(rs.getString(3));

                //把查询结果放入集合中
                list.add(dep);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return list;
    }
}
